<?php
if (!defined("_INDEX")) { header("Location: /"); die; }

$errors = array();
$poll_id = $route->getAdminId();

if ($route->POST("act") == "post")
{
	$user_id = $user->getUserId();
	$count = UV::readInt(strip_tags($route->POST("count")));
	$text = UV::readMQ(strip_tags($route->POST("text")));
	$order = UV::readInt(strip_tags($route->POST("order")));
	if (!$order)
		$order = 0;
	
	$db->sendQuery("delete from `answers` where `poll_id` = '$poll_id'");
	$db->sendQuery("delete from `user_answers` where `poll_id` = '$poll_id'");
	
	$u_poll = $db->sendQuery("update `polls` set `text` = '$text',`order` = '$order' where `poll_id` = '$poll_id' limit 1");
	if (!$u_poll)
		$errors[] = "Не удалось изменить текст опроса";
	
	$insert_str = "insert into `answers` values ";
	for ($i = 1; $i <= $count; $i++)
	{
		$answer = trim(UV::readMQ(strip_tags($route->POST("answer_$i"))));
		if ($answer != "")
		{
			$insert_str .= "(NULL,'$poll_id','$answer')";
			if ($i < $count)
				$insert_str .= ",";
		}
	}
	$answers = $db->sendQuery($insert_str);
	
	if (!$answers)
		$errors[] = "Не удалось изменить ответы к опросу";
	
	if (count($errors) == 0)
		goToPage("/admin/polls/view");
}

$poll = $db->getRow("select * from `polls` where `poll_id` = '$poll_id' limit 1");
$answers = $db->getTable("select * from `answers` where `poll_id` = '$poll_id' order by `answer_id`");
$u_answers = $db->getTable("select `answer_id`,count(`ua_id`) as `count` from `user_answers` where `poll_id` = '$poll_id' group by `answer_id`");
$ua_list = array();
foreach ($answers as $answer)
	$ua_list[$answer['answer_id']] = 0;
foreach($u_answers as $ua)
	$ua_list[$ua['answer_id']] = $ua['count'];

require_once ADMIN.DS.HEAD;
?>
<script type='text/javascript'>
$(document).ready(function(){
	var count = $("#count").val();
	$("#addAnswer").click(function(){
		count++;
		$("#count").val(count);
		$(".answer:last").after("<tr class='answer'><td><label for='answer_"+count+"'>Ответ "+count+"</label></td><td><input type='text' name='answer_"+count+"' id='answer_"+count+"' style='width:100%' /></td></tr>");
	})
	$("#delAnswer").click(function(){
		if (count > 2)
		{
			count--;
			$("#count").val(count);
			$(".answer:last").remove();
		}
	})
})
</script>
<div class='full'>
	<div class="submenu_container">
		<a class="submenu_button" href='/admin/report/look'>Просмотр отчетов</a>
		<a class="submenu_button" href='/admin/report/create'>Создать отчет</a>
		<a class="submenu_button" href='/admin/polls/view'>Просмотр опросов</a>
		<a class="submenu_button" href='/admin/polls/create'>Создать опрос</a>
		<a class="submenu_button submenu_button_active">Редактирование опроса</a>
		<a class="submenu_button" href='/admin/candidates/view'>Кандидаты</a>
	</div>
	<h3>Редактирование опроса</h3><?php
		if (count($errors) != 0)
			echo "<p style='color:red'>".implode("<br />",$errors)."</p>";
	?><p style='color:red'>Внимание! При редактировании опроса все результаты сбрасываются</p>
	<form method='post'>
		<input type='hidden' name='act' value='post' />
		<input type='hidden' id="count" name='count' value='<?php echo count($answers) ?>' />
		<table id='polls' cellspacing='10'>
			<tr>
				<td width='150px'><label for='order'>Приоритет</label></td>
				<td width='300px'><input type='text' id='order' name='order' style="width:20%;" value='<?php echo $poll['order']; ?>' /></td>
				<td width='70px'></td>
			</tr>
			<tr>
				<td><label for='text'>Текст вопроса</label></td>
				<td><input type='text' id='text' name='text' style="width:100%" value='<?php echo $poll['text']; ?>' /></td>
				<td></td>
			</tr><?php
			for ($i = 0; $i < count($answers); $i++)
			{
				?><tr class='answer'>
					<td><label for='answer_<?php echo $i+1; ?>'>Ответ <?php echo $i+1; ?></label></td>
					<td><input type='text' name='answer_<?php echo $i+1; ?>' id='answer_<?php echo $i+1; ?>' style="width:100%" value='<?php echo $answers[$i]['text']; ?>' /></td>
					<td style='font-size:9pt'>Ответов: <?php echo $ua_list[$answers[$i]['answer_id']]; ?></td>
				</tr><?php
			}
			?><tr>
				<td colspan='2' align='right'><input type='button' value="Удалить ответ" style="padding:5px 20px" id='delAnswer' />
				<input type='button' value="Добавить ответ" style="padding:5px 20px" id='addAnswer' /></td>
				<td></td>
			</tr>
			<tr>
				<td colspan='2' align='right'><input type='submit' style="padding:5px 20px" value="Редактировать опрос" /></td>
				<td></td>
			</tr>
	</form>
</div>

<?php require_once ADMIN.DS.FOOTER; ?>